<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/25
 * Time: 22:14
 */

namespace app\admin\model;
use app\common\model\BaseModel;

class LogisticalRrule extends BaseModel
{
    /**
     * @param $logisticalId
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 查询当前物流配送设置规则值
     */
    private function findLogisticalRrule($logistical_id){
       return self::where("logistical_id",'=',$logistical_id)->find();
    }

    /**
     * @param $data
     * @return LogisticalRrule
     * 更新商户当前物流费设置规则值
     */
    private function updateLogisticalRrule($data){

        $logistical_id = $data['logistical_id'];
        $data['updateTime'] = date("Y-m-d h:i:s",time());
        unset($data['logistical_id']);
        return self::where("logistical_id","=",$logistical_id)->update($data);
    }

    /**
     * @param $data
     * @return int|string
     * 创建当前商户物流费设置规则值
     */
    private function createRrule($data){
        $data['id'] = get_uniqid_id();
        return self::insert($data);
    }

    /**
     * @param array $data
     * @return LogisticalRrule|int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 保存当前商户设置物流费用值
     */
    public function saveRrule($data = [])
    {
        $findRrule = self::findLogisticalRrule($data['logistical_id']);
        if($findRrule){
            return self::updateLogisticalRrule($data);
        }else{
        return self::createRrule($data);
        }
    }

}